﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using PUCIT.eFCS.Utility.Encryption;
using System.Web.Configuration;
using PUCIT.eFCS.Business;

namespace PUCIT.eFCS
{
    public partial class ViewIncompleteTemplateList : System.Web.UI.Page
    {
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            HyperLink link1 = (HyperLink)e.Row.FindControl("HyperLink1");
            HyperLink link2 = (HyperLink)e.Row.FindControl("HyperLink2");


            if (link1 != null && link2 != null) // If its not the first row (headers of grid view)
            {

                /*
                 NameValueCollection queryStringParameters = new NameValueCollection();
                queryStringParameters.Add("documentId", e.Row.Cells[0].Text);

                // Encrypt query string parameters
                string encryptedString = CryptoQueryStringHandler.EncryptQueryStrings(queryStringParameters, WebConfigurationManager.AppSettings["CryptoKey"]);

                link1.NavigateUrl = "~/ViewFormTemplate.aspx?" + encryptedString;
                link2.NavigateUrl = "~/UpdateFormTemplate.aspx?" + encryptedString; 
                */


                NameValueCollection queryStringParameters = new NameValueCollection();
                queryStringParameters.Add("documentId", e.Row.Cells[0].Text);

                //Encrypt query string parameters
                string encryptedString = CryptoQueryStringHandler.EncryptQueryStrings(queryStringParameters, WebConfigurationManager.AppSettings["CryptoKey"]);


                Session["page"] = "UpdateFormTemplate.aspx";
                //string encryptedQueryStringPrameters = (string)Session["params"];

                //Session.Remove("params");

                // Decrypt query strings parameters
                //string cryptoKey = WebConfigurationManager.AppSettings["CryptoKey"];
                //string decryptedQueryString = CryptoQueryStringHandler.DecryptQueryStrings(encryptedQueryStringPrameters, cryptoKey);

                //string[] keyValuePair = decryptedQueryString.Split('='); // e.g. {documentId,15

                int documentId = int.Parse(e.Row.Cells[0].Text);
                BusinessClass eFCS = new BusinessClass();

                string documentName = eFCS.GetDocumentNameByDocumentId(documentId);
                string documentVersion = eFCS.GetDocumentVersionByDocumentId(documentId);
                string totalSections = eFCS.GetDocumentTotalSectionsByDocumentId(documentId);           //one number
                string completedSections = eFCS.GetDocumentCompletedSectionsByDocumentId(documentId);   //one number


                //FormNamelbl.Text = documentName;

                float version = float.Parse(documentVersion);
                Session["documentName"] = documentName;
                Session["version"] = version;
                Session["totalSections"] = totalSections;
                Session["completedSections"] = completedSections;


                link1.NavigateUrl = "~/ViewFormTemplate.aspx?" + encryptedString;
                link2.NavigateUrl = "~/DesignStationSections.aspx";
            
            }
        }
    }
}